﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ConjureBivouac2012.Object
{
    public class Entity
    {
        // Sprite
        public Sprite sprite;

        // check collision
        public const String ENNEMY = "ENNEMY";
        public const String PLAYER = "PLAYER";
        public const String BBULLET = "BULLET";
        public const String NBULLET = "NONE";
        public String myName;
        public String vulnerableTo;

        // Velocity
        protected Vector2 Velocity = new Vector2(0,0);
        protected Vector2 Direction = new Vector2(0, 0);

        // Animation info
        protected int animationCount;
        protected int animationDelay;

        protected TimeSpan lastTime;

        // CONSTRUCTOR
        public Entity()
        {
        }

        public virtual void Initialize()
        {
            sprite = new Sprite();
        }

        public virtual void LoadContent(ContentManager contentManager)
        {
            
        }

        // Update of the Entity
        public virtual void Update(GameTime time)
        {
            sprite.Position += Direction * Velocity * (float)time.ElapsedGameTime.TotalSeconds;
            StageCollision();
        }

        // Collision with the stage
        public void StageCollision()
        {
            if (this.sprite.Position.X + this.sprite.Size.Width/2 >= GameInfo.GetInstance().GetDevice().Viewport.Width ||
                this.sprite.Position.X - this.sprite.Size.Width / 2 <= 0)
            {
                Direction.X = -Direction.X;/*
                if (myName == NBULLET)
                {
                    myName = BBULLET;
                    vulnerableTo += ENNEMY;
                }*/
            }

            if (this.sprite.Position.Y + this.sprite.Size.Height / 2 >= GameInfo.GetInstance().GetDevice().Viewport.Height ||
                this.sprite.Position.Y - this.sprite.Size.Width / 2 <= 0)
            {
                Direction.Y = -Direction.Y;/*
                if (myName == NBULLET)
                {
                    myName = BBULLET;
                    vulnerableTo += ENNEMY;
                }*/
            }

        }

        // Adding an entity ?
        public virtual Entity[] addingEntity()
        {
            return null;
        }

        //Dropping entity ?
        public virtual Entity DroppingEntity()
        {
            return null;
        }

        // Destroy the entity !
        public virtual void destroyMe()
        {
        }

        // delete an entity ?
        public virtual bool hasToBeDestroyed()
        {
            return false;
        }

        // entity can collide ?
        public virtual bool canCollide()
        {
            return true;
        }

        // check string system canIt
        public bool canKill(Entity o) {return (String.Compare(o.vulnerableTo, myName) != 0);}

        // ReSetting the velocity
        public void setVelocity(Vector2 velocity)
        {
            this.Velocity.X = velocity.X;
            this.Velocity.Y = velocity.Y;
        }

        public Vector2 GetDirection() { return this.Direction; }

        public void SetDirection(Vector2 direction)
        {
            this.Direction = direction;
        }

        // ReSetting of the texture
        public void setTexture(Texture2D texture)
        {
            sprite.Texture = texture;
            
        }

        public int GetAnimationCount() { return this.animationCount; }

        public float GetRadius() { return (float)this.sprite.Size.Width/2; }
       
        // Drawing
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            sprite.Draw(spriteBatch);
        }
    }
}
